package lib;

public class Money {

    public static int DOT = 2;

    public static int muls() {
        int x = 1;
        int i = DOT;
        while (i > 0) {
            x *= 10;
            i--;
        }
        return x;
    }

    public static int dot() {
        if (DOT > 0) {
            return DOT + 1;
        }
        return 0;
    }

    public static String plusdot(String str) {
//        long round = (long) Math.ceil(Double.parseDouble(str));//TrungNH: round (float to long)
//        str = String.valueOf(round); 
        StringBuffer sb = new StringBuffer(str);
        int right = str.lastIndexOf('.');
        if (right == -1) {
            right = str.length();
        }
        char app = ',';
        for (int i = right - 1; i > 0; i--) {
            if (((right - i) % 3) == 0) {
                sb.insert(i, app);
            }
        }
        String s = sb.toString();
        if (sb.charAt(0) == '-' && s.indexOf(app) == 1) {
            sb.delete(1, 2);
        }
        return sb.toString();
    }

    public static String toWODString(long sum) {
        StringBuffer sb = new StringBuffer();
        sb.append(sum);
        boolean m = sum < 0;
        while (sb.length() < (m ? DOT + 2 : DOT + 1)) {
            sb.insert(m ? 1 : 0, '0');
        }
        if (DOT > 0) {
            sb.insert(sb.length() - DOT, '.');
        }
        return sb.toString();
    }

    public static String toString(long sum) {
        return plusdot(toWODString(sum));
    }

    public static String toStringDelDot(long sum) {
        String str = toString(sum);
        str = str.substring(0, str.length() - 3);
        return str;
    }

    public static String toStringNotDot(long sum) {
        sum = sum * 100;

        String str = plusdot(toWODString(sum));
        str = str.substring(0, str.length() - 3);
        return str;
    }

    public static long fromString(String str, int dots) {
        if (null == str) {
            return 0;
        }
        try {

            char app = ',';
            int i = 0;
            while ((i = str.indexOf(app)) > -1) {
                str = str.substring(0, i) + str.substring(i + 1);
            }


            StringBuffer sb = new StringBuffer(str);
            i = sb.length() - 1;
            while (i-- > 0) {
                if (sb.charAt(i) == ' ') {
                    sb.deleteCharAt(i);
                }
            }
            str = sb.toString();

            i = 0;
            while (i++ < dots) {
                sb.append('0');
            }
            int dot = str.indexOf('.');
            //if (dot == -1) dot = str.indexOf(',');
            if (dot != -1) {
                sb.deleteCharAt(dot);
                sb.setLength(dot + dots);
            }
            return Long.parseLong(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static long fromString(String str) {
        return fromString(str, DOT);
    }
}
